//
//  NSMutableString+TYSafe.h
//  TYFoundation
//
//  Created by 郭艺伟 on 2019/2/28.
//

#import <Foundation/Foundation.h>

#ifndef TYStringAppendString
#define TYStringAppendString(string,aString)                         \
if ((aString)) {                                               \
[(string) appendString:(aString)];                          \
}
#endif

#ifndef TYStringInsertStringAtIndex
#define TYStringInsertStringAtIndex(string,aString,index)                         \
if ((aString) && (index)<=[(string) length] && (index) >= 0) {                  \
[(string) insertString:(aString) atIndex:(index)];                          \
}
#endif

#ifndef TYStringSetString
#define TYStringSetString(string,aString)                         \
if ((aString)) {                                               \
[(string) setString:(aString)];                          \
}
#endif

#ifndef TYStringReplaceCharactersInRangeWithString
#define TYStringReplaceCharactersInRangeWithString(string,range,aString)                         \
if ((aString) && (range).location >= 0 && (range).length > 0 && (range).location < [(string) length] && NSMaxRange(range)<=[(string) length]) {                  \
[(string) replaceCharactersInRange:(range) withString:(aString)];                          \
}
#endif

#ifndef TYStringDeleteCharactersInRange
#define TYStringDeleteCharactersInRange(string,range)                         \
if (((range).location) >= 0 && (range).length > 0 && (range).location < [(string) length] && NSMaxRange(range)<=[(string) length]) {                  \
[(string) deleteCharactersInRange:(range)];                          \
}
#endif

#ifndef TYStringSubStringWithRange
#define TYStringSubStringWithRange(string,range,subString)                         \
if (((range).location) >= 0 && (range).length > 0  && (range).location <= [(string) length] && NSMaxRange(range)<=[(string) length]) {                  \
(subString) = [(string) substringWithRange:(range)];                          \
} else { \
(subString) = nil;\
}
#endif

#ifndef TYStringSubStringFromIndex
#define TYStringSubStringFromIndex(string,index,subString)                         \
if ((index)<[(string) length] && (index) >= 0) {                  \
(subString) = [(string) substringFromIndex:(index)];                          \
} else { \
(subString) = nil;\
}
#endif

#ifndef TYStringSubStringToIndex
#define TYStringSubStringToIndex(string,index,subString)                         \
if ((index)<=[(string) length] && (index) >= 0) {                  \
(subString) = [(string) substringToIndex:(index)];                          \
} else { \
(subString) = nil;\
}
#endif

@interface NSMutableString (TYSafe)

@end
